package cn.ac.siom.query.action;

import cn.ac.siom.query.service.ITab_xwlwService;
import cn.ac.siom.query.service.IaccessService;
import cn.ac.siom.query.util.action.DefaultAction;
import cn.ac.siom.query.vo.Accessinfo;
import cn.ac.siom.query.vo.Tab_xwlw;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@SuppressWarnings("unchecked")
@Controller
@RequestMapping(value = "/home/*")
public class Itab_wxlwAction extends DefaultAction {
    @Resource
    private ITab_xwlwService tab_xwlwService;
    @Resource
    private IaccessService iaccessService;

    @ResponseBody
    @RequestMapping(value="findNO_Tab_xwlw", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public List<Tab_xwlw> findNO(Integer id, HttpServletRequest request, Accessinfo accessinfo) throws SQLException {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        String url =request.getHeader("Referer");
        if(url==null){
            accessinfo.setUrl("/");
        }else{
                accessinfo.setUrl(url);
                accessinfo.setName("学位论文详情页");
        }
        accessinfo.setIp(ip);
        this.iaccessService.insertOne(accessinfo);
        List<Tab_xwlw>  tab_wxlw = this.tab_xwlwService.list_NO(id);
        return tab_wxlw;
    }


    @ResponseBody
    @RequestMapping(value="findPage_Tab_xwlw", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public Map<String, Object> findPage(HttpServletRequest request, Integer rownum, Integer page_id, Accessinfo accessinfo) throws SQLException {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        String url =request.getHeader("Referer");
        if(url==null){
            accessinfo.setUrl("list.html?allList=8");
        }else{
            if (url.contains("?")){
                accessinfo.setUrl(url);
                accessinfo.setName("首页学位论文");
                accessinfo.setIp(ip);
                this.iaccessService.insertOne(accessinfo);
            }
        }
        List<Tab_xwlw>  tab_wxlws = this.tab_xwlwService.list_page(rownum,page_id);
        Integer cont=this.tab_xwlwService.list_count();
        Map<String,Object> map = new HashMap<>();
        map.put("tab_wxlws",tab_wxlws);
        map.put("cont",cont);
        map.put("page_id",page_id);
        return map;
    }

    @ResponseBody
    @RequestMapping(value="findCount_Tab_xwlw", method= RequestMethod.GET,produces="application/json;charset=UTF-8")
    public int findCount() throws SQLException {
        return  this.tab_xwlwService.list_count();
    }


    @Override
    public String getText() {
        return null;
    }

}